package com.bo.lock.sync;

import org.openjdk.jol.info.ClassLayout;

/**
 * @Author: gpb
 * @Date: 2024/8/23 15:05
 * @Description:
 */
public class MarkWord {
    public static void main (String[] args) throws Exception {
        Object o = new Object();
        System.out.println(ClassLayout.parseInstance(o).toPrintable());
        Thread.sleep(5000);
        System.out.println(ClassLayout.parseInstance(o).toPrintable());
        new Thread(() -> {
            synchronized (o) {
                // 偏向锁
                System.out.println(Thread.currentThread().getName() + ": ");
                System.out.println(ClassLayout.parseInstance(o).toPrintable());
            }
        }).start();

        // mian - 偏向锁 - 轻量级锁CAS - 重量级锁
        synchronized (o) {
            // 偏向锁
            System.out.println(Thread.currentThread().getName() + ": ");
            System.out.println(ClassLayout.parseInstance(o).toPrintable());
        }
    }
//    java.lang.Object object internals:
//    OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
//      0     4        (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)
//            4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
//            8     4        (object header)                           00 10 00 00 (00000000 00010000 00000000 00000000) (4096)
//            12     4        (loss due to the next object alignment)
//    Instance size: 16 bytes
//    Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
//
//    java.lang.Object object internals:
//    OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
//      0     4        (object header)                           05 00 00 00 (00000101 00000000 00000000 00000000) (5)
//            4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
//            8     4        (object header)                           00 10 00 00 (00000000 00010000 00000000 00000000) (4096)
//            12     4        (loss due to the next object alignment)
//    Instance size: 16 bytes
//    Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
//
//    main:
//    java.lang.Object object internals:
//    OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
//      0     4        (object header)                           05 08 15 dd (00000101 00001000 00010101 11011101) (-585824251)
//            4     4        (object header)                           54 02 00 00 (01010100 00000010 00000000 00000000) (596)
//            8     4        (object header)                           00 10 00 00 (00000000 00010000 00000000 00000000) (4096)
//            12     4        (loss due to the next object alignment)
//    Instance size: 16 bytes
//    Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
//
//    Thread-0:
//    java.lang.Object object internals:
//    OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
//      0     4        (object header)                           02 79 e2 8a (00000010 01111001 11100010 10001010) (-1964869374)
//            4     4        (object header)                           54 02 00 00 (01010100 00000010 00000000 00000000) (596)
//            8     4        (object header)                           00 10 00 00 (00000000 00010000 00000000 00000000) (4096)
//            12     4        (loss due to the next object alignment)
//    Instance size: 16 bytes
//    Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
}
